﻿Public Class frmSSSDE
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmSSSDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                'Dim sssList As List(Of SSS) = sqlList.getItemSSS(Clipboard.GetData(DataFormats.Text))
                Dim sssList As List(Of SSS) = payrollManager.getItemSSS(Clipboard.GetData(DataFormats.Text))
                For Each sss As SSS In sssList
                    txtBraket.Text = sss.bracket
                    txtMinimumSalary.Text = sss.min_salary
                    txtMaximumSalary.Text = sss.max_salary
                    txtMonthlySalary.Text = sss.monthly_salary
                    txtEmployerShare.Text = sss.employer_share
                    txtEmployeeShare.Text = sss.employee_share
                    txtEmployeeCompensation.Text = sss.employee_compensation
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If

    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsEmptyString(txtBraket, "Bracket") Then Return
        If validator.IsNumericString(txtMinimumSalary, "Minimum Salary") Then Return
        If validator.IsNumericString(txtMaximumSalary, "Maximum Salary") Then Return
        If validator.IsNumericString(txtMonthlySalary, "Monthly Salary") Then Return
        If validator.IsNumericString(txtEmployeeShare, "Employer Share") Then Return
        If validator.IsNumericString(txtEmployerShare, "Employee Share") Then Return
        If validator.IsNumericString(txtEmployeeCompensation, "Employee Compensation") Then Return

        Try
            If Clipboard.GetData(DataFormats.Text) = "" Then
                'sqlManager.executeQuery("INSERT INTO tbl_sss (bracket,min_salary,max_salary,monthly_salary,employer_share,employee_share, employee_compensation) VALUE (" + txtBraket.Text + "," + txtMinimumSalary.Text + "," + txtMaximumSalary.Text + "," + txtMonthlySalary.Text + "," + txtEmployerShare.Text + "," + txtEmployeeShare.Text + "," + txtEmployeeCompensation.Text + ")")
                payrollManager.insertItemSSS(txtBraket.Text, txtMinimumSalary.Text, txtMaximumSalary.Text, txtMonthlySalary.Text, txtEmployeeShare.Text, txtEmployerShare.Text, txtEmployeeCompensation.Text)
                MsgBox("Successfully saved.")
            Else
                payrollManager.updateItemSSS(txtBraket.Text, txtMinimumSalary.Text, txtMaximumSalary.Text, txtMonthlySalary.Text, txtEmployeeShare.Text, txtEmployerShare.Text, txtEmployeeCompensation.Text, Clipboard.GetData(DataFormats.Text))
                'sqlManager.executeQuery("UPDATE tbl_sss SET bracket=" + txtBraket.Text + " , min_salary=" + txtMinimumSalary.Text + ", max_salary=" + txtMaximumSalary.Text + ", monthly_salary=" + txtEmployerShare.Text + ", employer_share=" + txtEmployerShare.Text + ", employee_share=" + txtEmployeeShare.Text + ", employee_compensation=" + txtEmployeeCompensation.Text + " WHERE id=" + Clipboard.GetData(DataFormats.Text) + " ")
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpSSS)
        Else
            Me.Close()
        End If

    End Sub
    Public Function ClearAllFields()
        validator.ResetControls(grpSSS)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function

    Private Sub grpSSS_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpSSS.Enter

    End Sub
End Class